package com.yanqu.road.server.manager.activity.doublespring;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.doublespring.DoubleSpringActivityConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivity;
import com.yanqu.road.server.manager.activity.daomu.pvp.DaoMuActivityPvp;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossDoubleSpringActivityMgr extends TempCrossMgr {

    //活动Map
    static Map<Integer, CrossDoubleSpringActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        List<Integer> acList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for (Integer activityId : activityMap.keySet()) {
            CrossDoubleSpringActivity activity = activityMap.get(activityId);
            // 展示期过3天后移除内存
            if (activity == null || activity.getDoubleSpringActivityConfig() == null || activity.getDoubleSpringActivityConfig().getActivityInfo() == null
                    || nowTime > activity.getDoubleSpringActivityConfig().getActivityInfo().getEndShowTime() * 1000 + 3 * DateHelper.DAY_MILLIONS) {
                acList.add(activityId);
            }
        }
        for (Integer activityId : acList) {
            activityMap.remove(activityId);
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        getLogger().info("CrossDoubleSpringActivityMgr reloadConfig start");
        for (CrossDoubleSpringActivity crossDoubleSpringActivity : activityMap.values()) {
            DoubleSpringActivityConfig activityConfig = crossDoubleSpringActivity.getDoubleSpringActivityConfig();
            if (activityConfig == null) {
                continue;
            }
            ActivityInfo activityInfo = activityConfig.getActivityInfo();
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                continue;
            }
            //刷新配置信息
            DoubleSpringActivityConfig doubleSpringActivityConfig = createDoubleSpringActivityConfig(activityInfo);
            crossDoubleSpringActivity.refreshConfig(doubleSpringActivityConfig);
            getLogger().info("CrossDoubleSpringActivityMgr reloadConfig success,activityId:{}", activityInfo.getActivityId());
        }
        getLogger().info("CrossDoubleSpringActivityMgr reloadConfig end");
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossActivityMapByType(eActivityType.DoubleSpring.getValue());
        //活动加载进内存
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (ActivityHelper.activityInShowTime(activityInfo)) {
                getActivity(activityInfo.getActivityId());
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for (CrossDoubleSpringActivity activity : activityMap.values()) {
            try {
                activity.save();
            }catch (Exception e){
                getLogger().error("doubleSpring activity {} save err", activity.getDoubleSpringActivityConfig().getActivityInfo().getActivityId(), e.getCause());
            }
        }
        return true;
    }

    /**
     * 构造活动配置数据
     * @param activityInfo
     * @return
     */
    private static DoubleSpringActivityConfig createDoubleSpringActivityConfig(ActivityInfo activityInfo) {
        //活动配置信息
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());
        return new DoubleSpringActivityConfig(activityInfo, activityConfigMap);
    }

    /**
     * 获取活动
     * @param activityId
     * @return
     */
    public static synchronized CrossDoubleSpringActivity getActivity(int activityId) {
        if (!activityMap.containsKey(activityId)) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if (activityInfo == null) {
                return null;
            }
            // 不在展示期也不处理了
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                return null;
            }
            //构造活动配置
            DoubleSpringActivityConfig doubleSpringActivityConfig = createDoubleSpringActivityConfig(activityInfo);
            //创建活动
            CrossDoubleSpringActivity activity = new CrossDoubleSpringActivity(doubleSpringActivityConfig);
            activityMap.put(activityId, activity);
        }
        return activityMap.get(activityId);
    }
}
